package com.chenfan.process.controller;

import com.chenfan.infra.annotation.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenfan.ccp.common.result.R;
import com.chenfan.process.biz.ApprovalFormBiz;
import com.chenfan.process.entity.dto.FormDTO;
import com.chenfan.process.entity.dto.FormQueryDTO;
import com.chenfan.process.entity.po.ProcessForm;
import com.chenfan.process.entity.validator.ProcessFormVO;
import com.chenfan.process.enums.BooleanEnum;

import io.swagger.annotations.ApiOperation;

/**
 * 表单API
 *
 * @author liuming
 * @date 2021/11/24
 */
@RestController
@RequestMapping("/form")
public class ApprovalFormController {

    @Autowired
    private ApprovalFormBiz approvalFormBiz;

    /**
     * 保存表单
     *
     * @param form
     *            表单
     * @return {@link R}<{@link Long}>
     */
    @Log(title = "创建表单")
    @ApiOperation(value = "保存表单")
    @PostMapping("")
    public R<Long> saveForm(@Validated @RequestBody FormDTO form) {
        return R.ok(approvalFormBiz.saveForm(form));
    }

    /**
     * 更新表单
     *
     * @param formId
     *            表单id
     * @param form
     *            表单
     * @return {@link R}<{@link Long}>
     */
    @Log(title = "更新表单")
    @ApiOperation(value = "更新表单")
    @PutMapping("/{formId}")
    public R<Long> updateForm(@PathVariable Long formId, @RequestBody FormDTO form) {
        form.setId(formId);
        return R.ok(approvalFormBiz.updateForm(form));
    }

    /**
     * 禁用表单
     *
     * @param formId
     *            表单的id
     * @return {@link R}<{@link Void}>
     */
    @Log(title = "禁用表单")
    @ApiOperation(value = "禁用表单")
    @PutMapping("{formId}/status/disable")
    public R<Void> disableForm(@PathVariable Long formId) {
        approvalFormBiz.changeStatus(formId, BooleanEnum.N.getCode());
        return R.ok();
    }

    /**
     * 启用表单
     *
     * @param formId
     *            表单的id
     * @return {@link R}<{@link Void}>
     */
    @Log(title = "启用表单")
    @ApiOperation(value = "启用表单")
    @PutMapping("{formId}/status/enable")
    public R<Void> enableForm(@PathVariable Long formId) {
        approvalFormBiz.changeStatus(formId, BooleanEnum.Y.getCode());
        return R.ok();
    }

    @Log(title = "删除表单")
    @ApiOperation(value = "删除表单")
    @DeleteMapping("{formId}")
    public R<Void> removeForm(@PathVariable Long formId) {
        approvalFormBiz.removeForm(formId);
        return R.ok();
    }

    /**
     * 表单详情
     *
     * @param formId
     *            表单的id
     * @return {@link R}<{@link ProcessFormVO}>
     */
    @ApiOperation(value = "表单详情")
    @GetMapping("/{formId}")
    public R<ProcessFormVO> formDetail(@PathVariable Long formId) {
        return R.ok(approvalFormBiz.formDetail(formId));
    }

    /**
     * 分页查询表单
     *
     * @param pageNum
     *            页面num
     * @param pageSize
     *            页面大小
     * @param form
     *            表单
     * @return {@link R}<{@link Page}<{@link ProcessForm}>>
     */
    @ApiOperation(value = "分页查询表单")
    @GetMapping("/page")
    public R<Page<ProcessForm>> page(@RequestParam(defaultValue = "1") Integer pageNum,
        @RequestParam(defaultValue = "20") Integer pageSize, FormQueryDTO form) {
        return R.ok(approvalFormBiz.page(pageNum, pageSize, form));
    }
}
